import { queryPerson, addPerson,removePerson,updatePerson,queryOrgUserTree,queryUserByOrgId} from '@/services/bnsConfig/person';
import {queryUserList,} from '@/services/system/sysUser';
import { Modal } from 'antd';
export default {
  namespace: 'person',

  state: {
    // 列表数据
    data: {
      dataList: [],
      pageNum: 0,
      pageSize: 0,
      totalCount: 0,
    },

    queryForm:{},
    treeDataList: [],
  },

  effects: {
    *queryList({ payload }, { call, put }) {
      const response = yield call(queryPerson, payload);
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *add({ payload, callback }, { call, put }) {
      const response = yield call(addPerson, payload);
      yield put({
        type: 'saveadd',
        payload: response,
      });
      if (callback) callback(response);
    },
    *remove({ payload, callback }, { call, put }) {
      const response = yield call(removePerson, payload);
      yield put({
        type: 'saveremove',
        payload: response,
      });
      if (callback) callback(response);
    },
    *update({ payload, callback }, { call, put }) {
      const response = yield call(updatePerson, payload);
      yield put({
        type: 'saveupdate',
        payload: response,
      });
      if (callback) callback(response);
    },
    *getTree({ payload, callback }, { call, put }) {
      const response = yield call(queryOrgUserTree, payload);
      yield put({
        type: 'saveTree',
        payload: response,
      });
      if (callback) callback(response);
    },
    *getList({ payload, callback }, { call, put }) {
      const response = yield call(queryUserByOrgId, payload);
      yield put({
        type: 'saveUserByOrgId',
        payload: response,
      });
      if (callback) callback(response);
    },
    *getListWithSub({ payload, callback }, { call, put }) {
      const response = yield call(queryUserList, payload);
      yield put({
        type: 'saveUserByOrgId',
        payload: response,
      });
      if (callback) callback(response);
    },
  },

  reducers: {
    save(state, action) {
      if(action.payload.rescode == 200){
        return {
        ...state,
        data: action.payload.data,
        };
      }else{
        Modal.error({
          content: action.payload.msg
        })
        return {
        ...state,
        data: [],
        };
      }
    },
    saveTree(state, action) {
      return {
        ...state,
        treeDataList: action.payload.data.dataList,
      };
    },
  },
};
